package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.service.OmsOrderReturnReasonService;
import com.macro.mapper.OmsOrderReturnReasonMapper;
import com.macro.model.OmsOrderReturnReason;
import com.macro.model.OmsOrderReturnReasonExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OmsOrderReturnReasonServiceImpl implements OmsOrderReturnReasonService {

    @Autowired
    private OmsOrderReturnReasonMapper reasonMapper;

    @Override
    public int create(OmsOrderReturnReason reason) {
        reason.setCreateTime(new Date());
        return reasonMapper.insert(reason);
    }

    @Override
    public int update(Long id, OmsOrderReturnReason reason) {
        OmsOrderReturnReasonExample example = new OmsOrderReturnReasonExample();
        example.createCriteria().andIdEqualTo(id);
        return reasonMapper.updateByExample(reason,example);
    }

    @Override
    public OmsOrderReturnReason getItem(Long id) {
        return reasonMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<OmsOrderReturnReason> list(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        OmsOrderReturnReasonExample example = new OmsOrderReturnReasonExample();
        example.setOrderByClause("sort desc");
        return reasonMapper.selectByExample(example);
    }

    @Override
    public int delete(List<Long> ids) {
        OmsOrderReturnReasonExample example = new OmsOrderReturnReasonExample();
        example.createCriteria().andIdIn(ids);
        return reasonMapper.deleteByExample(example);
    }

    @Override
    public int updateStatus(List<Long> ids, Integer status) {
        if (!status.equals(0) && !status.equals(1))
            return 0;
        OmsOrderReturnReason reason = new OmsOrderReturnReason();
        reason.setStatus(status);
        OmsOrderReturnReasonExample example = new OmsOrderReturnReasonExample();
        example.createCriteria().andIdIn(ids);

        return reasonMapper.updateByExampleSelective(reason,example);
    }
}
